﻿using HeinQi.Framework.DataAccess;
using HeinQi.Framework.LayerBases;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using PagedList;

namespace TbShop.Business.Components
{
    public class BaseBC<TEntity> : BusinessComponentsBase where TEntity : EntityBase
    {
        public IDataAccess<TEntity> dataAccess = null;

        public BaseBC(IDataAccess<TEntity> dataAccess)
        {
            this.dataAccess = dataAccess;
        }

        public TEntity Get(int id)
        {
            return this.dataAccess.Get(id);
        }

        public IPagedList<TEntity> GetPagedList(int pageIndex, int pageSize)
        {
            return this.dataAccess.GetAll().ToPagedList(pageIndex, pageSize);
        }

        public IQueryable<TEntity> GetAll()
        {
            return this.dataAccess.GetAll();
        }

        public IQueryable<TEntity> GetAll(int pageIndex, int pageSize, out int count)
        {
            return this.dataAccess.GetAll(pageIndex, pageSize, out count);
        }

        public IQueryable<TEntity> GetAll(IQueryable<TEntity> models, int pageIndex, int pageSize, out int count)
        {
            return this.dataAccess.GetAll(models, pageIndex, pageSize, out count);
        }

        public IQueryable<TEntity> GetAll(IOrderedQueryable<TEntity> models, int pageIndex, int pageSize, out int count)
        {
            return this.dataAccess.GetAll(models, pageIndex, pageSize, out count);
        }
        public bool Remove(TEntity item)
        {
            var deleteItem = this.dataAccess.Get(item.Id);

            if (item.VersionNumber == deleteItem.VersionNumber)
            {
                using (var t = new TransactionScope())
                {
                    this.dataAccess.Remove(deleteItem);
                    this.dataAccess.SaveChanges();
                    t.Complete();
                    return true;
                }
            }
            return false;
        }


        public bool Remove(int id, int versionNumber)
        {
            var item = this.dataAccess.Get(id);
            if (item != null)
            {
                if (item.VersionNumber == versionNumber)
                {
                    this.dataAccess.Remove(item);
                    using (var t = new TransactionScope())
                    {
                        this.dataAccess.SaveChanges();
                        t.Complete();
                    }
                    return true;
                }
            }
            return false;
        }

        public TEntity Save(TEntity item)
        {
            item = this.dataAccess.Save(item);
            using (var t = new TransactionScope())
            {
                this.dataAccess.SaveChanges();
                t.Complete();
            }
            return item;
        }

        public IQueryable<TEntity> SaveRange(IQueryable<TEntity> list)
        {
            foreach (var item in list)
            {
                this.dataAccess.Save(item);
            }
            using (var t = new TransactionScope())
            {
                this.dataAccess.SaveChanges();
                t.Complete();
            }
            return list;
        }
    }


    class RandomHelper
    {
        private static char[] constant =   
        {   
            '0','1','2','3','4','5','6','7','8','9',  
            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'   
        };
        private static Random rd = new Random();
        public static string GetRandom(int Length = 8)
        {
            lock (rd)
            {
                System.Text.StringBuilder newRandom = new System.Text.StringBuilder();
                for (int i = 0; i < Length; i++)
                {
                    newRandom.Append(constant[rd.Next(10)]);
                }
                return newRandom.ToString();
            }
        }
    }
}
